<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="算法名称">
              <j-input placeholder="请输入算法名称" v-model="queryParam.formulaName"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a-button @click="handleAdd" type="primary" icon="plus" style="margin-left: 8px">新增</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="false">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('算法库')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record"> 
          <a @click="handleEdit(record)" v-if="(userInfo.username === record.userId || userInfo.username === 'admin')">编辑</a><a disabled="false" v-else>编辑</a>
          <a-divider type="vertical" />
          <template v-if="(userInfo.username === record.userId || userInfo.username === 'admin')">
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a >删除</a>
          </a-popconfirm>
          </template>
          <a disabled="false" v-else>删除</a>
          <a-divider type="vertical" />
          <a @click="handleRun(record)">运算</a>
        </span>
        <span slot="formulaContent" slot-scope="text, record">
          <!-- {{ text.replaceAll('@','')}} -->
          <!-- style="max-width:1000px;white-space:normal; word-break:break-all;overflow:hidden;'" -->
          <div v-html="record.formulaContentHtml.replaceAll('wise','span style=\'border: 1px solid #dcdfe6;font-size:14px;margin: 0 6px; padding:2px 5px;\'')"></div> 
        </span>
        <span slot="formulaNameAction" slot-scope="text, record">
              <a @click="handleView(record)">{{record.formulaName}}</a>
        </span>
      </a-table>
    </div>

    <run-modal ref="runModal"></run-modal>
    <add-modal ref="addModal" @ok="addModalFormOk" />
    <edit-modal ref="editModal" @ok="modalFormOk" />
    <view-modal ref="viewModal" />
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import RunModal from './run'
import EditModal from './edit'
import AddModal from './add'
import ViewModal from './view'
import store from '@/store/'
import '@/assets/less/TableExpand.less'

export default {
  name: 'UserFormulaList',
  mixins: [JeecgListMixin],
  components: {
    RunModal,
    EditModal,
    AddModal,
    ViewModal
  },
  data() {
    return {
      description: '算法库管理页面',
      // 表头
      columns: [
        // {
        //   title: '#',
        //   dataIndex: '',
        //   key: 'rowIndex',
        //   width: 60,
        //   align: 'center',
        //   customRender: function (t, r, index) {
        //     return parseInt(index) + 1
        //   },
        // },
        {
          title: '算法ID',
          align: 'left',
          dataIndex: 'id',
          // fixed: 'left',
          width: 120,
        },
        {
          title: '算法名称',
          align: 'left',
          // fixed: 'left',
          scopedSlots: { customRender: 'formulaNameAction' },
          // dataIndex: 'formulaName',
          // customRender: function (text) {
          //   return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          // }, 
        },
        {
          title: '内容',
          align: 'left',
          dataIndex: 'formulaContent', 
          scopedSlots: { customRender: 'formulaContent' },
          // customRender: function (text) {
          //   return text.replaceAll('@','');
          // },
        },
        {
          title: '修改日期',
          align: 'center',
          dataIndex: 'modifyDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
        },
        // {
        //   title: '创建人',
        //   align: 'center',
        //   dataIndex: 'userId',
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/userFormula/userFormula/list',
        delete: '/userFormula/userFormula/delete',
        deleteBatch: '/userFormula/userFormula/deleteBatch',
        exportXlsUrl: '/userFormula/userFormula/exportXls',
        importExcelUrl: 'userFormula/userFormula/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
     userInfo() {
        return this.$store.getters.userInfo
      }
  },
  methods: {
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'formulaName', text: '公式名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'formulaContent', text: '内容', dictCode: '' })
      fieldList.push({ type: 'date', value: 'modifyDate', text: '修改日期' })
      fieldList.push({ type: 'string', value: 'remark', text: '备注', dictCode: '' })
      fieldList.push({ type: 'int', value: 'seqNo', text: '序号，用于公式排序', dictCode: '' })
      fieldList.push({ type: 'string', value: 'userId', text: '创建人', dictCode: '' })
      this.superFieldList = fieldList
    },
    handleAdd() {
      // this.$router.push({ path: '/userFormula/add/0' })
      this.$refs.addModal.show(0)
    },
    handleEdit: function (record) {
      this.$refs.editModal.show(record.id)
      //this.$router.push({ path: `/userFormula/add/${record.id}` })
    },
     handleView: function (record) {
      this.$refs.viewModal.show(record.id)
      //this.$router.push({ path: `/userFormula/add/${record.id}` })
    },
    handleRun: function (record) {
      this.$refs.runModal.show(record)
      return
      var that = this
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一个公式！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }

        that.$refs.runModal.show(this.selectionRows[0])
      }
    },
    addModalFormOk(formulaId){
      this.loadData();
      this.$refs.editModal.show(formulaId)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>